<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Admin extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->lang->load('breadcrumb', 'estonian');
        if ($this->session->userdata('is_admin') == false) {
            $this->session->set_flashdata('error', 'Administraatori lehekülje kasutamiseks logige palun vastavate õigustega sisse.');
            redirect('login');
            die();
        }
        $this->load->model('Competition_model', 'CompetitionModel');
        $this->load->model('Team_model', 'TeamModel');
        $this->load->model('Athlete_model', 'AthleteModel');
    }

    public function index() {
        redirect("admin/home");
    }

    public function home() {
        $data['a_href'] = $this->session->flashdata('a_href');
        $data['a_text'] = $this->session->flashdata('a_text');
        $data['message'] = $this->session->flashdata('message');
        $this->load->template('admin/home', $data);
    }

    public function tables() {
        $this->load->model('Fina_model', 'FinaModel');
        $this->load->model('Rank_model', 'RankModel');
        $this->load->model('Rudolph_model', 'RudolphModel');

        $fina_table = $this->FinaModel->fetch_fina_admin_table();

        foreach ($fina_table as $row) {
            $fina[$row->year][$row->pool] = $row->no;
        }

        $data['fina'] = $fina;

        $rudolph_table = $this->RudolphModel->fetch_rudolph_admin_table();
        foreach ($rudolph_table as $row) {
            $rudolph[$row->year][$row->age] = $row->no;
        }
        $data['rudolph'] = $rudolph;

        $rank_table = $this->RankModel->fetch_rank_admin_table('M');
        foreach ($rank_table as $row) {
            $event = $row->distance . ' ' . Utils::est_stroke($row->stroke, false, true);
            $ranks_m[$event][$row->type] = $this->Utils->ms_to_result_string($row->time_ms);
        }

        $data['ranks_m'] = $ranks_m;

        $rank_table = $this->RankModel->fetch_rank_admin_table('F');
        foreach ($rank_table as $row) {
            $event = $row->distance . ' ' . Utils::est_stroke($row->stroke, false, true);
            $ranks_f[$event][$row->type] = $this->Utils->ms_to_result_string($row->time_ms);
        }

        $data['ranks_f'] = $ranks_f;

        $this->load->template('admin/tables', $data);
    }

    public function deleteAll() {
        $this->db->empty_table('athletes');
        $this->db->empty_table('competitions');
        $this->db->empty_table('teams');
        $this->db->empty_table('results');
        $this->db->empty_table('relays');
        $this->db->empty_table('splits');
        $this->db->empty_table('events');
        $this->db->empty_table('search_queries');

        redirect('admin');
    }

    public function deleteResults() {

        $this->db->empty_table('results');
        $this->db->empty_table('relays');
        $this->db->empty_table('splits');
        $this->db->empty_table('events');

        redirect('admin');
    }

    public function addSwimStyles() {

        $swimstyle_id = 1001;
        $swimstyles = array(
            array('swimstyle_id' => $swimstyle_id++, 'distance' => 25, 'relaycount' => 1, 'stroke' => 'free'),
            array('swimstyle_id' => $swimstyle_id++, 'distance' => 50, 'relaycount' => 1, 'stroke' => 'free'),
            array('swimstyle_id' => $swimstyle_id++, 'distance' => 100, 'relaycount' => 1, 'stroke' => 'free'),
            array('swimstyle_id' => $swimstyle_id++, 'distance' => 200, 'relaycount' => 1, 'stroke' => 'free'),
            array('swimstyle_id' => $swimstyle_id++, 'distance' => 400, 'relaycount' => 1, 'stroke' => 'free'),
            array('swimstyle_id' => $swimstyle_id++, 'distance' => 800, 'relaycount' => 1, 'stroke' => 'free'),
            array('swimstyle_id' => $swimstyle_id++, 'distance' => 1500, 'relaycount' => 1, 'stroke' => 'free'),
            array('swimstyle_id' => $swimstyle_id++, 'distance' => 25, 'relaycount' => 1, 'stroke' => 'back'),
            array('swimstyle_id' => $swimstyle_id++, 'distance' => 50, 'relaycount' => 1, 'stroke' => 'back'),
            array('swimstyle_id' => $swimstyle_id++, 'distance' => 100, 'relaycount' => 1, 'stroke' => 'back'),
            array('swimstyle_id' => $swimstyle_id++, 'distance' => 200, 'relaycount' => 1, 'stroke' => 'back'),
            array('swimstyle_id' => $swimstyle_id++, 'distance' => 25, 'relaycount' => 1, 'stroke' => 'breast'),
            array('swimstyle_id' => $swimstyle_id++, 'distance' => 50, 'relaycount' => 1, 'stroke' => 'breast'),
            array('swimstyle_id' => $swimstyle_id++, 'distance' => 100, 'relaycount' => 1, 'stroke' => 'breast'),
            array('swimstyle_id' => $swimstyle_id++, 'distance' => 200, 'relaycount' => 1, 'stroke' => 'breast'),
            array('swimstyle_id' => $swimstyle_id++, 'distance' => 25, 'relaycount' => 1, 'stroke' => 'fly'),
            array('swimstyle_id' => $swimstyle_id++, 'distance' => 50, 'relaycount' => 1, 'stroke' => 'fly'),
            array('swimstyle_id' => $swimstyle_id++, 'distance' => 100, 'relaycount' => 1, 'stroke' => 'fly'),
            array('swimstyle_id' => $swimstyle_id++, 'distance' => 200, 'relaycount' => 1, 'stroke' => 'fly'),
            array('swimstyle_id' => $swimstyle_id++, 'distance' => 100, 'relaycount' => 1, 'stroke' => 'medley'),
            array('swimstyle_id' => $swimstyle_id++, 'distance' => 200, 'relaycount' => 1, 'stroke' => 'medley'),
            array('swimstyle_id' => $swimstyle_id++, 'distance' => 400, 'relaycount' => 1, 'stroke' => 'medley'),
            array('swimstyle_id' => $swimstyle_id++, 'distance' => 25, 'relaycount' => 4, 'stroke' => 'free'),
            array('swimstyle_id' => $swimstyle_id++, 'distance' => 50, 'relaycount' => 4, 'stroke' => 'free'),
            array('swimstyle_id' => $swimstyle_id++, 'distance' => 100, 'relaycount' => 4, 'stroke' => 'free'),
            array('swimstyle_id' => $swimstyle_id++, 'distance' => 200, 'relaycount' => 4, 'stroke' => 'free'),
            array('swimstyle_id' => $swimstyle_id++, 'distance' => 25, 'relaycount' => 4, 'stroke' => 'back'),
            array('swimstyle_id' => $swimstyle_id++, 'distance' => 50, 'relaycount' => 4, 'stroke' => 'back'),
            array('swimstyle_id' => $swimstyle_id++, 'distance' => 100, 'relaycount' => 4, 'stroke' => 'back'),
            array('swimstyle_id' => $swimstyle_id++, 'distance' => 25, 'relaycount' => 4, 'stroke' => 'breast'),
            array('swimstyle_id' => $swimstyle_id++, 'distance' => 50, 'relaycount' => 4, 'stroke' => 'breast'),
            array('swimstyle_id' => $swimstyle_id++, 'distance' => 100, 'relaycount' => 4, 'stroke' => 'breast'),
            array('swimstyle_id' => $swimstyle_id++, 'distance' => 25, 'relaycount' => 4, 'stroke' => 'fly'),
            array('swimstyle_id' => $swimstyle_id++, 'distance' => 50, 'relaycount' => 4, 'stroke' => 'fly'),
            array('swimstyle_id' => $swimstyle_id++, 'distance' => 100, 'relaycount' => 4, 'stroke' => 'fly'),
            array('swimstyle_id' => $swimstyle_id++, 'distance' => 50, 'relaycount' => 4, 'stroke' => 'medley'),
            array('swimstyle_id' => $swimstyle_id++, 'distance' => 100, 'relaycount' => 4, 'stroke' => 'medley'),
            array('swimstyle_id' => $swimstyle_id++, 'distance' => 25, 'relaycount' => 8, 'stroke' => 'free'),
            array('swimstyle_id' => $swimstyle_id++, 'distance' => 50, 'relaycount' => 8, 'stroke' => 'free'),
            array('swimstyle_id' => $swimstyle_id++, 'distance' => 25, 'relaycount' => 8, 'stroke' => 'medley'),
            array('swimstyle_id' => $swimstyle_id++, 'distance' => 50, 'relaycount' => 8, 'stroke' => 'medley'),
        );

        print_r($swimstyles);
//        Uncomment next line to insert swimstyles into empty swimstyle database.
//        $this->db->insert_batch('swimstyle', $swimstyles);
    }

    public function competitions($manual = false, $new = 0) {
        $data['country_input'] = $this->Utils->country_dropdown('country', array('EE', 'LV', 'LT', 'FI', 'SE', 'DE', 'FR', 'RU', 'RS', 'NL'));
        if ($manual == 'manual') {
            if ($new === 'new') {
                $this->load->template('admin/manual_competition_new', $data);
            } else if ($new === 'add') {
                $competition_id = $this->CompetitionModel->insertNewCompetition(
                        $this->input->post('name'), $this->input->post('city'), $this->input->post('country'), $this->input->post('facility'), $this->input->post('pool'), $this->input->post('start_date'), $this->input->post('end_date')
                );

                $this->session->set_flashdata('message', 'Võistluse lisamine õnnestus.');
                $this->session->set_flashdata('a_href', site_url("competitions/summary/$competition_id"));
                $this->session->set_flashdata('a_text', 'Ava lisatud võistlus');
                redirect("admin/home");
            } else if ($new != 0 && is_numeric($new)) {
                $data['competition'] = $this->CompetitionModel->fetch($new);
                $data['teams'] = $this->TeamModel->fetchAll();
                $data['athletes'] = $this->AthleteModel->fetchAllWithResults();
                $this->load->template('admin/manual_results', $data);
            } else {
                $data['competitions'] = $this->CompetitionModel->fetchAll();
                $this->load->template('admin/manual_competition', $data);
            }
        } else {
            $this->load->template('admin/competitions', $data);
        }
    }
    
    public function competition_ajax($id = false) {
        
        if ($id != false) {
            $competition = $this->CompetitionModel->fetch($id);
        }
        
    }

}